package com.lemon.smartsafety.http;

import android.text.TextUtils;
import android.util.Log;

import com.lemon.smartsafety.utils.LogUtils;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;

/**
 * 项目名称：workspace
 * 类描述：
 * 创建人：Fargo
 * 创建时间：2016/9/12 10:46
 * 修改人：Fargo
 * 修改时间：2016/9/12 10:46
 * 修改备注：
 */
public class NewLoggingInterceptor  implements Interceptor {

    public static final String TAG = "OkHttpUtils";
    private String tag;
    private boolean showResponse;

    public NewLoggingInterceptor(String tag, boolean showResponse)
    {
        if (TextUtils.isEmpty(tag))
        {
            tag = TAG;
        }
        this.showResponse = showResponse;
        this.tag = tag;
    }

    @Override
    public Response intercept(Interceptor.Chain chain) throws IOException {
        Request request = chain.request();


        Response response = null;
        try {
            long t1 = System.nanoTime();
            LogUtils.LOGE(String.format("Sending request %s on %s%n%s", request.url(),
                    chain.connection(), request.headers()));

            response = chain.proceed(request);
            long t2 = System.nanoTime();
            LogUtils.LOGE(String.format("Received response for %s in %.1fms%n%s",
                    response.request().url(), (t2 - t1) / 1e6d, response.headers()));
            //logForResponse(response);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return response;
    }


    /**
     * 输出日志
     * @param response
     */
    private void logForResponse(Response response)
    {
        try
        {
            //===>response log
            Log.e(tag, "========response'log=======");
            Response.Builder builder = response.newBuilder();
            Response clone = builder.build();
            Log.e(tag, "url : " + clone.request().url());
            Log.e(tag, "code : " + clone.code());
            Log.e(tag, "protocol : " + clone.protocol());
            if (!TextUtils.isEmpty(clone.message()))
                Log.e(tag, "message : " + clone.message());

            if (showResponse)
            {
                ResponseBody body = clone.body();
                if (body != null)
                {
                    MediaType mediaType = body.contentType();
                    if (mediaType != null)
                    {
                        Log.e(tag, "responseBody's contentType : " + mediaType.toString());
                        if (isText(mediaType))
                        {
                            String resp = body.string();
                            Log.e(tag, "responseBody's content : " + resp);
                        } else
                        {
                            Log.e(tag, "responseBody's content : " + " maybe [file part] , too large too print , ignored!");
                        }
                    }
                }
            }

            Log.e(tag, "========response'log=======end");
        } catch (Exception e)
        {
            e.printStackTrace();
        }

    }


    private boolean isText(MediaType mediaType)
    {
        if ((mediaType.type() != null && mediaType.type().equals("text")) || "application/x-www-form-urlencoded".equals(mediaType.toString()))
        {
            return true;
        }
        if (mediaType.subtype() != null)
        {
            if (mediaType.subtype().equals("json") ||
                    mediaType.subtype().equals("xml") ||
                    mediaType.subtype().equals("html") ||
                    mediaType.subtype().equals("webviewhtml")
                    )
                return true;
        }
        return false;
    }



}